from django.db import models

# Create your models here.
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager
from django.db.models import UniqueConstraint


# 学校列表
class School(models.Model):
    name = models.CharField(max_length=150,
                            verbose_name="学校名称",
                            unique=True)
    def __str__(self):
        return self.name

    class Meta:
        verbose_name= "学校"
        verbose_name_plural = "学校"

class College(models.Model):
    name = models.CharField(max_length=150,
                            verbose_name="学院名称")
    short_name = models.CharField(max_length=150,
                            null=True,
                            verbose_name="学院简称")
    code = models.CharField(max_length=60, 
                            verbose_name="学院代码")
    active = models.BooleanField(default=True, 
                                 verbose_name="活跃状态",
                                 help_text="学院撤销、更名场景下置为无效状态")

    school = models.ForeignKey('School', on_delete=models.PROTECT)
    

    def __str__(self):
        return self.name

    class Meta:
        verbose_name= "学院"
        verbose_name_plural = "学院"
        constraints = [
            UniqueConstraint(name="School_college",
                            fields=['school', 'name'])
        ]


# $todo: 增加修改时间、创建时间等信息记录
# 备注可以统一写入一个log表中，仅保留logid
# logid的构成: model_name + id + name + updatetime的hash值
class Major(models.Model):
    college = models.ForeignKey('College',
                                on_delete=models.PROTECT)
    name = models.CharField(max_length=120,
                            verbose_name="专业名称")
    short_name = models.CharField(max_length=120,
                            null=True,
                            verbose_name="专业简称")
    code = models.CharField(max_length=60,
                            verbose_name="专业代码")
    active = models.BooleanField(default=True, 
                                verbose_name="活跃状态",
                                help_text="专业撤销、更名场景下置为无效状态")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name= "专业"
        verbose_name_plural = "专业"
        constraints = [
            UniqueConstraint(name="college_major",
                            fields=['college', 'name'])
        ]

# 去掉跟学校专业的强关联
class StudentClass(models.Model):
    major = models.ForeignKey('Major',
                              on_delete=models.PROTECT)
    name = models.CharField(max_length=60,
                            verbose_name="班级名称")
    short_name = models.CharField(max_length=20,
                                null=True,
                                verbose_name="班级简称")

    start_year = models.CharField(max_length=4,
                            null=True,
                            verbose_name="年级")

    def get_major(self):
        return self.major.name
    
    def get_college(self):
        return self.major.college.name
    
    def get_school(self):
        return self.major.college.school.name

    def __str__(self):
        return self.major.name + ' ' + self.name + '-' + self.start_year

    class Meta:
        verbose_name= "班级"
        verbose_name_plural = "班级"
        constraints = [
            UniqueConstraint(name="major_class",
                        fields=['major', 'name'])
        ]

class StudentManager(BaseUserManager):
    def create_user(student_id, student_name, password, **kwargs):
        try:
            student = Student(student_id=student_id,
                              student_name=student_name)
        except Exception:
            raise ValueError('user exist')

        student.set_password(password)

        # $todo: 处理传入专业/班级的情况
        return student

    def create_superuser(student_id, password=None, **kwargs):
        return None

class Student(AbstractBaseUser):
    GNEDER_CHOICES = (
        (0, 'Undefined'),
        (1, 'Male'),
        (2, 'Female'),
    )
    student_id = models.CharField(max_length=15, unique=True, primary_key=True)
    student_name = models.CharField(max_length=120)
    gender = models.IntegerField(choices=GNEDER_CHOICES, default=0)

    college = models.CharField(max_length=120, default="")
    major = models.CharField(max_length=120, default="")
    student_class = models.ForeignKey('StudentClass', on_delete=models.PROTECT, null=True)

    USERNAME_FIELD = 'student_id'
    REQUIRED_FIELDS = ['student_name']

    class Meta:
        verbose_name= "学生"
        verbose_name_plural = "学生"

    def __str__(self):
        return f'{self.student_id}: {self.student_name}'
